package com.edu.gcu.manager.service.impl;

import com.edu.gcu.manager.service.IProductService;
import com.edu.gcu.manager.vo.ProductVo;
import com.edu.gcu.system.common.Const;
import com.edu.gcu.system.common.ResponseCode;
import com.edu.gcu.system.common.ServerResponse;
import com.edu.gcu.system.dao.ProductMapper;
import com.edu.gcu.system.pojo.Product;
import com.edu.gcu.system.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author polo
 * @version V1.0
 * @description: 产品service接口实现类
 * @date 2018-02-20-12:22
 */

@Service("iProductService")
public class IProductServiceImpl implements IProductService {

    private Logger logger = LoggerFactory.getLogger(IProductServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;

    private ServerResponse publicReturn(String msg, int resultCount) {
        if (resultCount > 0) {
            return ServerResponse.createByErrorMessage(msg + "成功!");
        }
        return ServerResponse.createByErrorMessage(msg + "失败!");
    }

    @Override
    public ServerResponse delete(String id, String productIds) {
        if (StringUtils.isBlank(productIds)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        List<String> productIdList = Splitter.on(";").splitToList(productIds);
        for (String productId : productIdList) {
            Product product = new Product();
            product.setId(productId);
            product.setUpdateBy(id);
            product.setStatus(Const.ProductStatusEnum.IS_DEL.getCode());
            int resultCount = productMapper.updateByPrimaryKeySelective(product);
            if (resultCount > 0) {
                continue;
            }
            return ServerResponse.createByErrorMessage("删除失败!");
        }
        return ServerResponse.createByErrorMessage("删除成功!");
    }

    @Override
    public ServerResponse update(String id, Product product) {
        int resultCount = productMapper.updateByPrimaryKeySelective(product);
        return this.publicReturn("更新", resultCount);
    }

    @Override
    public ServerResponse change(String id, String productId, Integer status) {
        if (StringUtils.isBlank(productId)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        try {
            status = Const.ProductStatusEnum.codeOf(status).getCode();
        } catch (RuntimeException e) {
            logger.error("exception is ", e);
            return ServerResponse.createByErrorMessage("状态码错误,更新失败");
        }
        Product product = new Product();
        product.setId(productId);
        product.setUpdateBy(id);
        product.setStatus(status);
        int resultCount = productMapper.updateByPrimaryKeySelective(product);
        return this.publicReturn("更新", resultCount);
    }

    @Override
    public ServerResponse add(String id, Product product) {
        product.setId(UuidUtil.getUUID());
        product.setSecurityCode(UuidUtil.getUUID());
        product.setStatus(Const.ProductStatusEnum.ON_SALE.getCode());
        product.setCreateBy(id);
        product.setUpdateBy(id);
        int resultCount = productMapper.insertSelective(product);
        return this.publicReturn("插入", resultCount);
    }

    @Override
    public ServerResponse detail(String id) {
        if (StringUtils.isBlank(id)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        Product product = productMapper.selectByPrimaryKey(id);
        if (product != null) {
            ProductVo productVo = this.assembleProductVo(product);
            return ServerResponse.createBySuccess(productVo);
        }
        return ServerResponse.createByErrorMessage("查找失败!");
    }

    @Override
    public ServerResponse<PageInfo> list(String name, Integer status, String orderBy, Integer pageNum,
                                         Integer pageSize) {

        // 分页与排序
        PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isNotBlank(orderBy)) {
            if (Const.ProductGiftOrderBy.PRODUCT_GIFT_DESC_ASC.contains(orderBy)) {
                List<String> orderByList = Splitter.on("_").splitToList(orderBy);
                PageHelper.orderBy(orderByList.get(0) + " " + orderByList.get(1));
            }
        }

        List<Product> productList = productMapper.selectList(name, status);
        List<ProductVo> productVoList = Lists.newArrayList();
        for (Product product : productList) {
            ProductVo productVo = this.assembleProductVo(product);
            productVoList.add(productVo);
        }
        PageInfo pageInfo = new PageInfo<>(productList);
        pageInfo.setList(productVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    private ProductVo assembleProductVo(Product product) {
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        productVo.setStatusDesc(Const.ProductStatusEnum.codeOf(product.getStatus()).getValue());
        return productVo;
    }
}


